export default {
	//数据
	data() {
		return {
			countDownTime:0,//倒计时
      validate: {}, //验证对象
      validate2: {}, //验证对象
      code: 0,
      id: 0
		}
	},
	created() { //调用方法 forgetForm
	},
	mounted() {
		// 内容版块获取浏览器尺寸
		var autoDivHeight = document.getElementById('Login');
		var deviceWidth = document.documentElement.clientWidth;
		var deviceHeight = document.documentElement.clientHeight;
		autoDivHeight.style.width = (Number(deviceWidth) -0) + 'px'; //数字是页面布局宽度差值
		autoDivHeight.style.height = (Number(deviceHeight) - 120) + 'px'; //数字是页面布局高度差

    jQuery.validator.addMethod("isMobile", function(value, element) {
      let length = value.length;
      let mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
      return this.optional(element) || (length == 11 && mobile.test(value));
    }, "请正确填写手机号码");
    //加载表单验证
    this.validate = $("#forgetForm").validate({
      ignore: [],
      rules: {
        userAccount: { // 身份证
          required: true,
          maxlength: 18,
          minlength: 18
        },
        receiver: { // 手机
          required: true,
          isMobile : true
        },
        userPassword: { // mi ma
          required: true,
          maxlength: 16,
          minlength: 6
        },
        userPassword2: {
          required: true,
          maxlength: 16,
          minlength: 6
        },
        verification: {
          required: true
        }

      },
      messages: {

        userAccount: { // userAccount
          required: "请输入身份证",
          maxlength: "请输入长度小于19",
          minlength: '最小长度不小于18'
        },
        receiver: { // 手机号
          required: "请输入手机号",
          isMobile : '请输入正确的手机号码!'
        },

        userPassword: { // 密码
          required: "请输入新密码",
          maxlength: '密码长度不能大于16',
          minlength: '密码长度不能小于6'
        },
        userPassword2: { // 密码
          required: "请再次输入新密码",
          maxlength: '密码长度不能大于16',
          minlength: '密码长度不能小于6'
        },
        verification: {
          required: '验证码不能为空!'
        }

      },
    /*  errorElement: "em",
      errorPlacement: function(error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });

   /* this.validate2 = $("#forgetForm").validate({
      ignore: [],
      rules: {
        userAccount: { // 身份证
          required: true,
          maxlength: 18,
          minlength: 18
        },
        receiver: { // 手机
          required: true,
          isMobile : true
        },
      },
      messages: {

        userAccount: { // userAccount
          required: "请输入身份证",
          maxlength: "请输入长度小于19",
          minlength: '最小长度不小于18'
        },
        receiver: { // 手机号
          required: "请输入手机号",
          isMobile : '请输入正确的手机号码!'
        },

      },
     /!* errorElement: "em",
      errorPlacement: function(error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*!/
    });*/

	},
	methods: { //写函数方法
		/*进入首页*/
		jumpPage: function() {
			this.$router.push({
				path: "/index",
			})
		},
		/*发送验证码*/
		sendVerificationCode:function(){
			let that = this;
      that.code = 0;
      that.id = 0;
      let userAccount = $('#userAccount').val();
      let receiver = $('#receiver').val();

      if(!userAccount){
        layer.msg('请输入身份证!', {
          time: 3000, //3s后自动关闭
        });
        return;
      }else if(userAccount.length >18 || userAccount.length <18){
        layer.msg('请输入正确的身份证!', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      let mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
      if(!mobile.test(receiver)){
        layer.msg('请输入正确的手机号!', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
   /*  let flg = that.validate2.form();
      if(! flg){
        return;
      }*/
			that.countDownTime = 10;
      let timer = setInterval(function(){
				that.countDownTime-=1;
				if(!that.countDownTime){
					clearInterval(timer);
				}
			},1000)



      that.$post( // 发送验证码
        that.urls.getVerification,
        {"userAccount": userAccount ,"receiver": receiver }
      )
        .then(function(res) {
          if(res.success) {
            layer.msg('验证码发送成功!', {
              time: 3000, //3s后自动关闭
            });
            that.code = res.data.userPhoneNumber;
            that.id = res.data.id;

            setTimeout(function () {
              that.code = 0;
            }, 60000 * 5);

          }else {
            layer.msg(res.message, {
              time: 3000, //3s后自动关闭
            });
          }
        })

		},
    
    // 提交
    subForm: function () {
      let that = this;

      let flg = that.validate.form();
      if(! flg){
        return;
      }

      let userPassword = $('#userPassword').val();
      let userPassword2 = $('#userPassword2').val();
      let verification = $('#verification').val();

       if(userPassword != userPassword2){
         layer.msg('两次输入的密码不一致!', {
           time: 3000, //3s后自动关闭
         });

       }else if(!that.code){
         layer.msg('验证码已失效!', {
           time: 3000, //3s后自动关闭
         });
       }else if(verification != that.code){
         layer.msg('验证码不正确!', {
           time: 3000, //3s后自动关闭
         });
       } else {
         let data = that.combinationFormObject(that.validate.currentForm);
         // 提交更改密码
         that.$post(
           that.urls.editUser,
           {"id": that.id,"userPassword": data.userPassword, "roleName": "xxx"}
         )
           .then(function(res) {
             if(res.success) {
               layer.msg('成功更改密码,3秒后自动跳转到登录页面!', {
                 time: 3000, //3s后自动关闭
               });
               setTimeout(function () {
                 that.$router.push({
                   path: "/login",
                 })
               }, 3000);
             }else {
               layer.msg(res.message, {
                 time: 3000, //3s后自动关闭
               });
             }
           })
       }

    }
    
    
	}
}
